﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PASNew.UserControls
{
    public partial class BackendUC : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserIdentity"] != null)
            {
                if (IsPostBack == false)
                {
                    UserDetailsDto user = (UserDetailsDto)Session["UserIdentity"];

                    using (PASDataDataContext data = new PASDataDataContext())
                    {
                        var Alevel = (from a in data.UserDetails
                                      where a.UserId == user.UserID
                                      select a).FirstOrDefault();

                        if (Alevel != null)
                        {
                            if (Alevel.UserLevel != 1)
                            {
                                Response.Redirect("Home.aspx");
                            }
                            else
                            {
                                var loadCompanies = from c in data.CompanyDetails
                                                    select new ListItem
                                                    {
                                                        Text = c.CompanyName,
                                                        Value = c.CompanyId.ToString(),
                                                    };
                                foreach (ListItem l in loadCompanies)
                                {
                                    DDLoadCompanies.Items.Add(l);
                                }
                            }
                        }
                    }
                }
            }
        }

        protected void DDLoadCompanies_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLoadNames.Items.Clear();
            int numbers = 0;
            int compID = int.Parse(DDLoadCompanies.SelectedValue.ToString());

            using (PASDataDataContext data = new PASDataDataContext())
            {
                var loadRelationships = from c in data.CompanyUserRelations
                                        where c.CompanyId == compID
                                        select new ListItem
                                        {
                                            Value = c.UserId.ToString(),
                                        };
                foreach (ListItem ls in loadRelationships)
                {

                    var loadNames = from d in data.UserDetails
                                    where d.UserId == int.Parse(ls.Value)
                                    select new ListItem
                                    {
                                        Text = d.UserName,
                                        Value = d.UserId.ToString(),
                                    };


                    foreach (ListItem l in loadNames)
                    {
                        DDLoadNames.Items.Add(l);
                        numbers++;
                    }

                    lblNumber.Text = "Number of users: " + numbers.ToString();
                    lblNumber.Visible = true;
                }
            }
        }
    }
}